const service = require('../../utils/service.js')
const api = require('../../utils/util.js')
Page({
  data: {
    userInfo: '',
    shopInfo: '',
    order_id: 53,
    orderInfo: {},
    cancelReason: '',
    showOther: false,
    reason_id: '',
    otherReasonText: '',
    showPon: false,
  },

  onLoad: function(options) {
    wx.showLoading({
      title: '正在加载...',
    })
    if (options) {
      this.setData({
        order_id: options.order_id
      })
    }
    let shopInfo = wx.getStorageSync('shopInfo')
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      userInfo: userInfo,
      shopInfo: shopInfo
    })
    this.init()
  },
  init: function() {
    let that = this
    this.getCancelReasonList();
    api.ajax(service.url('orderDetail'), {
        order_id: that.data.order_id
      },
      function(res) {
        if (res.code == 0) {
          that.setData({
            orderInfo: res.data
          })
          wx.hideLoading()
        } else {
          api.toast(res.message)
        }
      })
  },

  // 去付款
  payOrder: function(e) {
    let money = e.currentTarget.dataset.money
    let pay_key = e.currentTarget.dataset.key
    wx.navigateTo({
      url: `../pay/pay?pay_key=${pay_key}&money=${money}`,
    })
  },

  //显示取消订单原因
  showLos: function() {
    this.setData({
      showPon: true
    })
  },

  hideLos: function() {
    this.setData({
      showPon: false
    })
  },

  //阻止穿透
  move: function() {

  },

  // 取消订单原因
  getCancelReasonList: function() {
    let that = this
    api.ajax(service.url('getCancelReasonList'), {}, function(res) {
      that.setData({
        cancelReason: res
      })
    })
  },

  // 选择取消原因
  radioChange: function(e) {
    let select = e.detail.value
    this.setData({
      otherReasonText: ''
    })

    if (select == 3) {
      this.setData({
        showOther: true,
        reason_id: select
      })
    } else if (select == 1) {
      this.setData({
        showOther: false,
        reason_id: select,
        otherReasonText: '不想要了'
      })
    } else if (select) {
      this.setData({
        showOther: false,
        reason_id: select,
        otherReasonText: '拍错了'
      })
    }
  },

  // 获取其他理由
  getOther: function(e) {
    let str = e.detail.value
    this.setData({
      otherReasonText: str
    })
  },

  // 取消订单
  cancelOrder: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id

    if (this.data.reason_id == '' || this.data.reason_id == null) {
      api.toast('请选择取消原因')
      return
    }
    let other_reason = this.data.otherReasonText
    if (other_reason == '' || other_reason == null) {
      api.toast('请填写理由')
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定取消该订单?',
      success: (rel) => {
        if (rel.confirm) {
          api.ajax(service.url('cancelOrder'), {
              order_id: that.data.order_id,
              order_reason_id: that.data.reason_id,
              other_reason: other_reason
            },
            function(res) {
              if (res.code == 0) {
                api.toast('取消订单成功')
                setTimeout(function() {
                  var pages = getCurrentPages();
                  var currPage = pages[pages.length - 1];   //当前页面
                  var prevPage = pages[pages.length - 2];  //上一个页面
                  prevPage.init();
                  wx.navigateBack({
                    delta: -1
                  })
                }, 1500)
              } else {
                api.toast(res.message)
              }
            })
        }
      }
    })
  },

  // 删除订单
  deleteOrder: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确认删除该订单吗？',
      success: function(res) {
        if (res.confirm) {
          api.ajax(service.url('delOrder'), {
              order_id: id
            },
            function(res) {
              if (res.code == 0) {
                api.toast('删除订单成功')
                setTimeout(function() {
                  getCurrentPages()[getCurrentPages().length - 2].init()
                  wx.navigateBack({
                    delta: -1
                  })
                }, 800)
              } else {
                api.toast(res.message)
              }
            })
        }
      }
    })
  },

  // 延长收货
  extendCollect: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    api.ajax(service.url('setCustomerDelay'), {
        order_id: id
      },
      function(res) {
        console.log(res)
        if (res.code == 0) {
          api.toast('已延长收货时间')
        } else {
          api.toast(res.message)
        }
      })
  },

  // 撤销申请
  cancelRefun: function(e) {
    let that = this
    let refunid = e.currentTarget.dataset.refunid
    api.ajax(service.url('undoRefund'), {
        order_return_id: refunid
      },
      function(res) {
        console.log(res)
        if (res.code == 0) {
          api.toast('成功撤销退货、退款申请')
        } else {
          api.toast(res.message)
        }
      })
  },

  // 提醒发货
  reminderSlip: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    api.ajax(service.url('reminderSlip'), {
        order_id: id
      },
      function(res) {
        if (res.code == 0) {
          api.toast('已提醒商家发货')
        } else {
          api.toast(res.message)
        }
      })
  },

  // 确认收货
  getGoods: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确认收到货物?',
      success: function(rel) {
        if (rel.confirm) {
          api.ajax(service.url('orderReceived'), {
            order_id: id
          }, function(res) {
            if (res.code == 0) {
              api.toast('收货成功')
              getCurrentPages()[getCurrentPages().length - 2].init()
              setTimeout(function() {
                wx.navigateBack({
                  delta: -1
                });
              }, 800)
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },

  //去评价
  toComment: function(e) {
    wx.navigateTo({
      url: '../comment_goods_order/comment_goods_order?order_id=' + e.currentTarget.dataset.id,
      success: function(res) {},
    })
  },

  // 申请退款
  toRefun: function(e) {
    let type = e.currentTarget.dataset.type
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../refun/refun?type=${type}&order_id=${id}`,
    })
  },

  // 退款详情
  toRefunDetail: function(e) {
    let refunId = e.currentTarget.dataset.refunid;
    let orderId = this.data.orderInfo.id;
    wx.navigateTo({
      url: `../refun_detail/refun_detail?refun_id=${refunId}&order_id=${orderId}`,
    })
  },

  // 查看物流
  toLog: function(e) {
    let order_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../flow/flow?order_id=${order_id}`,
    })
  },

  // 复制订单号
  copy: function() {
    let that = this
    wx.setClipboardData({
      data: that.data.orderInfo.order_sn,
      success: (res) => {
        console.log(res)
      }
    })
  },

  // 前往商品详情
  toGoods: function(e) {
    let goods_id = e.currentTarget.dataset.goodsid
    wx.navigateTo({
      url: `../goods_detail/goods_detail?goods_id=${goods_id}`,
    })
  },

  // 下载app
  downloadApp: function() {
    wx.showModal({
      title: '提示',
      content: '下载APP更方便联系客服，是否下载?',
      confirmText: '下载',
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '../download_app/download_app',
          })
        }
      }
    })
  },
  onShareAppMessage: function(e) {
    let goods_id = e.target.dataset.goodsid
    let img = e.target.dataset.img
    if (e.from == 'button') {
      return {
        title: '来和我一起拼团吧',
        path: 'pages/goods_detail/goods_detail?goods_id=' + goods_id,
        imageUrl: img
      }
    }
  }
})